<template>
  <div>
    <el-table :data="specsList" border style="width: 100%">
      <el-table-column prop="id" label="规格编号" width="180">
      </el-table-column>
      <el-table-column prop="specsname" label="规格名称" width="180">
      </el-table-column>
      <el-table-column prop="attrs" label="规格属性" width="180">
        <template slot-scope="items">
          <el-button plain v-for="item in items.row.attrs" :key="item">
            {{ item }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="item">
          <div>
            <el-tag effect="dark" type="success" v-if="item.row.status == 1"
              >启用</el-tag
            >
            <el-tag effect="dark" type="warning" v-else>禁用</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="item">
          <div>
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="edit(item.row.id)"
            ></el-button>
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-delete"
              @click="del(item.row.id)"
            ></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      background
      layout="prev, pager, next"
      @current-change="changepage"
      :page-size="pageSize"
      :total="count"
      style="margin-top: 15px; float: right; margin-right: 10px"
    >
    </el-pagination>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import { specsdelete } from "../../require/api";
export default {
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      specsList: "specs/getSpecs",
      pageSize: "specs/getSize",
      count: "specs/getCount",
    }),
  },
  methods: {
    ...mapActions({
      getSpecsAction: "specs/getSpecsAction",
      getCountAction: "specs/getCountAction",
      changePageAction: "specs/changePageAction",
    }),
    // 修改
    edit(id) {
      // 传参
      this.$emit("edit", id);
    },
    // 删除
    del(id) {
      this.$confirm("你确定要删除这条数据吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 接口删除
          specsdelete({ id }).then((res) => {
            if (res.code == 200) {
              this.getSpecsAction();
              this.getCountAction();
              //成功提示
              this.$message.success(res.msg);
            } else {
              //失败提示
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //分页点击的当前页
    changepage(p) {
      // console.log(p);
      // 传参给更改页码切换页面的方法
      this.changePageAction(p);
    },
  },
  mounted() {
    this.getSpecsAction();
    this.getCountAction();
    // this.$store.dispatch("specs/getSpecsAction");
  },
};
</script>
<style lang=less scoped >
.el-button--default {
  width: 45px;
  height: 30px;
  font-size: 12px;
  padding: 3px;
  box-sizing: border-box;
}
</style>
